package com.qingyun.customview.nestscroll1

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
import com.qingyun.customview.databinding.ActivityMyNestscrollviewBinding
import com.qingyun.customview.databinding.MySimpleListItem1Binding

class MyNestScrollViewActivity : AppCompatActivity() {
    companion object {
        fun start(context: Context) {
            Intent(context, MyNestScrollViewActivity::class.java).also {
                context.startActivity(it)
            }
        }
    }

    private lateinit var binding: ActivityMyNestscrollviewBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        binding = ActivityMyNestscrollviewBinding.inflate(layoutInflater)
        super.onCreate(savedInstanceState)
        setContentView(binding.root)

        initRecyclerView()
    }

    private fun initRecyclerView() {
        (1..50).map { "item $it" }.toList().also {
            binding.recyclerView.adapter = SimpleAdapter(it)
        }
    }


    class SimpleAdapter(
        private val data: List<String>,
    ) : RecyclerView.Adapter<SimpleAdapter.ViewHolder>() {
        override fun onCreateViewHolder(
            parent: ViewGroup,
            viewType: Int,
        ): ViewHolder {
            return ViewHolder.create(parent)
        }

        override fun onBindViewHolder(
            holder: ViewHolder,
            position: Int,
        ) {
            holder.bindData(data[position])
        }

        override fun getItemCount(): Int {
            return data.size
        }

        class ViewHolder(
            private val binding: MySimpleListItem1Binding,
        ) : RecyclerView.ViewHolder(binding.root) {
            fun bindData(string: String) {
                binding.text1.text = string
            }

            companion object {
                fun create(parent: ViewGroup): ViewHolder {
                    val binding = MySimpleListItem1Binding.inflate(
                        LayoutInflater.from(parent.context),
                        parent,
                        false
                    )
                    return ViewHolder(binding)
                }
            }
        }
    }

}